package Application.serviceImpl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import Application.dao.CollectDao;
import Application.dao.EssayDao;
import Application.entity.Collect;
import Application.entity.Essay;
import Application.entity.Result;
import Application.service.EssayService;

@Service("EssayService")
public class EssayServiceImpl implements EssayService{

	@Resource
	private EssayDao dao;
	
	@Resource
	private CollectDao dao2;

	@Override
	public Result insertEssay(Essay essay) {
		Result r = new Result();
		try {
			String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
			essay.setDate(time);
			dao.insertEssay(essay);
			r.setContent(1);
		} catch (Exception e) {
			r.setContent(0);
		}
		return r;
	}

	@Override
	public List<Essay> findEssayList() {
		return dao.findEssayList();
	}

	@Override
	public Result deleteEssayById(int id) {
		Result r = new Result();
		try {
			dao.deleteEssayById(id);
			r.setContent(1);
		} catch (Exception e) {
			r.setContent(0);
		}
		return r;
	}

	@Override
	public Essay findEssayById(int id) {
		return dao.findEssayById(id);
	}

	@Override
	public List<Essay> vague(String value) {
		return dao.vague(value);
	}

	@Override
	public List<Essay> findCollectEssay(int collectorid) {
		List<Essay> list=new ArrayList();
		List<Collect> l=dao2.findUserCollect(collectorid);
		for(Collect c: l ) {
			list.add(dao.findEssayById(c.getEssayid()));
		}
		return list;
	}

	@Override
	public List<Essay> findEssayByAuthor(int authorid) {
		return dao.findEssayByAuthor(authorid);
	}

	
}
